package com.example.idachuappone.order.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.idachuappone.R;
import com.example.idachuappone.order.entity.ImageItem;
import com.example.idachuappone.order.imageutil.Bimp;
import com.example.idachuappone.order.imageutil.BitmapCache;
import com.example.idachuappone.order.imageutil.BitmapCache.ImageCallback;

public class ImageGridAdapter extends BaseAdapter {
	BitmapCache cache;
	ImageCallback callback;
	private Context context;
	private List<ImageItem> dataList;
	private Handler mHandler;
	public Map<String, String> map;
	private int selectTotal;
	private TextCallback textcallback;

	class AnonymousClass_2 implements OnClickListener {
		private final/* synthetic */ViewHolder val$holder;
		private final/* synthetic */ImageItem val$item;
		private final/* synthetic */int val$position;

		AnonymousClass_2(int i, ImageItem imageItem, ViewHolder viewHolder) {
			this.val$position = i;
			this.val$item = imageItem;
			this.val$holder = viewHolder;
		}

		@Override
		public void onClick(View v) {
			boolean z = false;
			String path = ImageGridAdapter.this.dataList.get(this.val$position).imagePath;
			ImageGridAdapter imageGridAdapter;
			if (Bimp.drr.size() + ImageGridAdapter.this.selectTotal < 10) {
				this.val$item.isSelected = !this.val$item.isSelected;
				if (this.val$item.isSelected) {
					this.val$holder.selected.setImageResource(R.drawable.album_image_yes);
					imageGridAdapter = ImageGridAdapter.this;
					imageGridAdapter.selectTotal = imageGridAdapter.selectTotal + 1;
					if (ImageGridAdapter.this.textcallback != null) {
						ImageGridAdapter.this.textcallback.onListen(ImageGridAdapter.this.selectTotal,
								ImageGridAdapter.this.dataList);
					}
					ImageGridAdapter.this.map.put(path, path);
				} else if (!this.val$item.isSelected) {
					this.val$holder.selected.setImageResource(R.drawable.album_image_no);
					imageGridAdapter = ImageGridAdapter.this;
					imageGridAdapter.selectTotal = imageGridAdapter.selectTotal - 1;
					if (ImageGridAdapter.this.textcallback != null) {
						ImageGridAdapter.this.textcallback.onListen(ImageGridAdapter.this.selectTotal,
								ImageGridAdapter.this.dataList);
					}
					ImageGridAdapter.this.map.remove(path);
				}
			} else if (Bimp.drr.size() + ImageGridAdapter.this.selectTotal < 10) {
			} else {
				if (this.val$item.isSelected) {
					ImageItem imageItem = this.val$item;
					if (!this.val$item.isSelected) {
						z = true;
					}
					imageItem.isSelected = z;
					this.val$holder.selected.setImageResource(R.drawable.album_image_no);
					imageGridAdapter = ImageGridAdapter.this;
					imageGridAdapter.selectTotal = imageGridAdapter.selectTotal - 1;
					ImageGridAdapter.this.map.remove(path);
					return;
				}
				Message.obtain(ImageGridAdapter.this.mHandler, 0).sendToTarget();
			}
		}
	}

	public static interface TextCallback {
		void onListen(int i, List<ImageItem> list);
	}

	private class ViewHolder {
		private ImageView iv;
		private ImageView selected;

		private ViewHolder() {
		}
	}

	public ImageGridAdapter(Context context, List<ImageItem> dataList, Handler mHandler) {
		this.textcallback = null;
		this.map = new HashMap();
		this.selectTotal = 0;
		this.callback = new ImageCallback() {
			@Override
			public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params) {
				if (imageView != null && bitmap != null) {
					String url = (String) params[0];
					if (url != null && url.equals(imageView.getTag())) {
						imageView.setImageBitmap(bitmap);
					}
				}
			}
		};
		this.context = context;
		this.dataList = dataList;
		this.mHandler = mHandler;
		this.cache = new BitmapCache();
	}

	public void setDataList(List<ImageItem> dataList) {
		this.dataList = dataList;
	}

	public void setTextCallback(TextCallback listener) {
		this.textcallback = listener;
	}

	@Override
	public int getCount() {
		return this.dataList == null ? 0 : this.dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.dataList == null ? null : (ImageItem) this.dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LinearLayout.inflate(this.context, R.layout.simple_image_grid, null);
			holder.iv = (ImageView) convertView.findViewById(R.id.image);
			holder.selected = (ImageView) convertView.findViewById(R.id.isselected);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ImageItem item = this.dataList.get(position);
		holder.iv.setTag(item.imagePath);
		this.cache.displayBmp(holder.iv, item.thumbnailPath, item.imagePath, this.callback);
		if (item.isSelected) {
			holder.selected.setImageResource(R.drawable.album_image_yes);
		} else {
			holder.selected.setImageResource(R.drawable.album_image_no);
		}
		holder.selected.setOnClickListener(new AnonymousClass_2(position, item, holder));
		holder.iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
		return convertView;
	}
}